package filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import bean.Goods;
import dao.GetGoodsCount;
import dao.ListGoods;

/**
 * Servlet Filter implementation class AdminListView
 */
public class AdminListView implements Filter {

    /**
     * Default constructor. 
     */
    public AdminListView() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		String string = httpServletRequest.getParameter("page");
		int page = Integer.parseInt(string);
		
		int count = GetGoodsCount.getCount("SELECT count(*) FROM goods where goods_state=1");
		int pagecount = (count-1)/25+1;
		if(page==-1 || page>pagecount)
			page=pagecount;
		if(page==0)
			page=1;
		List<Goods> list;
		list = ListGoods.getListGoods("select * from goods where goods_state=1 order by goods_id desc limit "+(page-1)*25+",25");//执行具体获取方法

		request.setAttribute("list", list);//将结果放入request
		request.setAttribute("page", page);
		
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
